// �������������еĽڵ�
package Leetcode;

class solution_024 {
	public ListNode swapPairs(ListNode head) {
		if (head == null || head.next == null)
			return head;
		ListNode first = head;
		ListNode second = head.next;
		ListNode pre = second;
		while (second != null & first != null) {
			if (second.next == null || second.next.next == null) {
				first.next = second.next;
				second.next = first;
				first = first.next;
				second = null;
			} else {
				ListNode index = second.next;
				first.next = second.next.next;
				second.next = first;
				first = index;
				second = index.next;
			}
		}
		return pre;
	}
}

public class Solution024 {
	public ListNode swapPairs(ListNode head) {
		if (head == null || head.next == null)
			return head;
		ListNode dirtyNode = new ListNode(0);
		dirtyNode.next = head;
		ListNode index = dirtyNode;
		while (index.next != null && index.next.next != null) {
			ListNode node1 = index.next;
			ListNode node2 = index.next.next;
			index.next = node2;
			node1.next = node2.next;
			node2.next = node1;
			index = node1;
		}
		return dirtyNode.next;
	}
}
